<?php
/**
 * 后台订单表操作模型
 * @author lifw
 */
class MOrder extends MY_Model {
	protected $table = 'order';

    public function timesecond($seconds)//将秒时间转换具体时间
    {
        $seconds = (int)$seconds;
        if( $seconds>3600 ){
            if( $seconds>24*3600 ){
                $days        = (int)($seconds/86400);
                $days_num    = $days."天";
                $seconds    = $seconds%86400;//取余
            }
            $hours = intval($seconds/3600);
            $minutes = $seconds%3600;//取余下秒数
            $time = $days_num.$hours."小时".gmstrftime('%M分钟%S秒', $minutes);
        }else{
            $time = gmstrftime('%H小时%M分钟%S秒', $seconds);
        }
        return $time;
    }

    public function getList($param, $user_id,$perPage){
        $this->db->select( 'o.*,(o.return_time - o.create_time) used_second,FROM_UNIXTIME(o.create_time) as create_date,c.name as car_name,c.picture as car_picture, c.price as car_price',false);
        $this->db->distinct();
        $this->db->from($this->table.' o');
        $this->db->join('car c','o.car_id = c.id','INNER');
        $this->db->where('o.user_id',$user_id);
        if ($param['status'] !== 4){
            $this->db->where('o.status', $param['status']);
            $this->db->where('o.pay_status',$param['pay_status']);
        }
        $this->db->limit($perPage,$perPage*($param['page'] - 1));
        $list = $this->db->get()->result();
        foreach ($list as $v){
            $v->used_second = $this->timesecond($v->used_second);
        }
        return $list;
    }

    public function getAdminList($limit){
        $this->db->select( 'o.*,u.avatar,u.nick_name,c.picture,c.name,w.out_trade_no');
        $this->db->distinct();
        $this->db->from($this->table.' o');
        $this->db->join('user u','o.user_id = u.id','INNER');
        $this->db->join('car c','o.car_id = c.id','INNER');
        $this->db->join('wx_pay w','o.id = w.oid','LEFT');
        $this->db->limit($limit['limit'],$limit['offset']);
        $this->formfilter->doFilter();
        $this->db->group_by('o.id');
        $query = $this->db->get();
        return $query->result();
    }

    public function getCount(){
        $this->db->select('count(distinct o.id) as num');
        $this->db->from($this->table.' o');
        $this->db->join('user u','o.user_id = u.id','INNER');
        $this->db->join('car c','o.car_id = c.id','INNER');
        $this->db->join('wx_pay w','o.id = w.oid','INNER');
        $this->formfilter->doFilter();
        $tmp = $this->db->get()->result();
        return (int)$tmp[0]->num;
    }

	public function getDetail($oid){
        $this->db->select( 'o.*,u.avatar,u.nick_name,u.id as uid,c.picture,c.name,w.out_trade_no,u.create_time as u_create_time,u.sex,c.status as c_status,c.brand');
        $this->db->distinct();
        $this->db->from($this->table.' o');
        $this->db->join('user u','o.user_id = u.id','INNER');
        $this->db->join('car c','o.car_id = c.id','INNER');
        $this->db->join('wx_pay w','o.id = w.oid','LEFT');
        $this->db->where('o.id',$oid);
        $this->db->group_by('o.id');
        $query = $this->db->get();
	    return $query->row();
    }

    public function getApiDetail($oid){
        $this->db->select('o.*,FROM_UNIXTIME(o.create_time) as create_date,FROM_UNIXTIME(o.pay_time) as pay_date,c.name as car_name,c.picture as car_picture, c.price as car_price',false);
        $this->db->from($this->table.' o');
        $this->db->join('car c','o.car_id = c.id','INNER');
        $this->db->where('o.id',$oid);
        $query = $this->db->get();
        return $query->row();
    }

    public function getById($id)
    {
        $query = $this->db->where('id', $id)->get($this->table);
        return $query->row();
    }

    public function getStaticData($begin_date, $end_date)
    {
        $format = 'Y-m-d';
        $type = 'days';
        $end = false;
        if ($begin_date) {
            $begin = strtotime($begin_date);
            $end = strtotime($end_date.' 23:59:59');
            if ($end - $begin > (3600 * 24 * 30)){
                $format = 'Y-m';
                $type = 'months';
                $times = floor(($end - $begin)/(3600 * 24 * 30)) + 1;
                $end += (3600 * 24 * 30);
            }else{
                $times = floor(($end - $begin)/(3600 * 24 )) + 1;
                $end += (3600 * 24);
            }
            $x = IOSS_StaticConfig::get_weeks($end,$format,$times,$type);
        } else {
            $x = IOSS_StaticConfig::get_weeks();
        }
        if ($type == 'days'){
            $begin_time = strtotime($x[0] . ' 00:00:00');
            $end_time = strtotime($x[count($x) - 1] . ' 23:59:59');
        }else{
            $begin_time = strtotime($x[0] . ' 00:00:00');
            $end_time = $end?$end:time();
        }

        // 获取用户的易测/难测量
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where('create_time >= ' . $begin_time);
        $this->db->where('create_time <= ' . $end_time);
        $list = $this->db->get()->result();
        $arr = array();
        $arr['line'] = $x;
        if (count($list)) {
            foreach ($x as $k => $v) {
                $arr['num'][$k] = 0;
                $arr['ing_num'][$k] = 0;
                $arr['apply_num'][$k] = 0;
                $arr['wait_pay'][$k] = 0;
                $arr['complete'][$k] = 0;
                $arr['amount'][$k] = 0;
                foreach ($list as $value) {
                    if ($v == date($format,$value->create_time)) {
                        $arr['num'][$k] += 1;
                        if ($value->status == 0){
                            $arr['ing_num'][$k]  += 1;
                        }
                        if ($value->status == 1){
                            $arr['apply_num'][$k]  += 1;
                        }
                        if ($value->status == 2 && $value->pay_status == 0){
                            $arr['wait_pay'][$k]  += 1;
                        }
                        if ($value->status == 2 && $value->pay_status == 1){
                            $arr['complete'][$k]  += 1;
                            $arr['amount'][$k]  += $value->amount;
                        }
                    }
                }
            }
        }
        return $arr;
    }

    public function getInfo($begin,$end){
        $this->db->select('count(id) as total_num, sum(amount) as total_amount,
            sum(case when status = 2 and pay_status = 0 then 1 else 0 end ) as wait_num,
            sum(case when status = 2 and pay_status = 0 then amount else 0 end ) as wait_amount,
            sum(case when status = 2 and pay_status = 1 then 1 else 0 end ) as payed_num,
            sum(case when status = 2 and pay_status = 1 then amount else 0 end ) as payed_amount,
            ',false);
        $this->db->from($this->table);
        if ($begin){
            $this->db->where('create_time >= ' . strtotime($begin));
        }
        if ($end){
            $this->db->where('create_time <= ' . strtotime($end.' 23:59:59'));
        }
        return $this->db->get()->row();
    }

    public function getTotal(){
        $this->db->select("count(id) as num,sum(amount) as amount,
            sum(case when status = 2 and pay_status = 1 then amount else 0 end ) as payed_amount,
        ");
        $this->db->from($this->table);
        return $this->db->get()->row();
    }

    public function getWeek(){
        $be = IOSS_StaticConfig::get_b_and_e();
        $this->db->select("count(id) as num,sum(amount) as amount,
            sum(case when status = 0 then 1 else 0 end ) as ing_num,
            sum(case when status = 0 then amount else 0 end ) as ing_amount,
            sum(case when status = 2 and pay_status = 1 then 1 else 0 end ) as payed_num,
            sum(case when status = 2 and pay_status = 0 then 1 else 0 end ) as wait_num,
            sum(case when status = 2 and pay_status = 1 then amount else 0 end ) as payed_amount,
            sum(case when status = 2 and pay_status = 0 then amount else 0 end ) as wait_amount,
        ");
        $this->db->from($this->table);
        $this->db->where('create_time >= '.$be[0]);
        $this->db->where('create_time <= '.$be[1]);
        return $this->db->get()->row();
    }

    public function getMonth(){
        $be = IOSS_StaticConfig::get_this_month();
        $this->db->select("count(id) as num,sum(amount) as amount,
            sum(case when status = 0 then 1 else 0 end ) as ing_num,
            sum(case when status = 0 then amount else 0 end ) as ing_amount,
            sum(case when status = 2 and pay_status = 1 then 1 else 0 end ) as payed_num,
            sum(case when status = 2 and pay_status = 0 then 1 else 0 end ) as wait_num,
            sum(case when status = 2 and pay_status = 1 then amount else 0 end ) as payed_amount,
            sum(case when status = 2 and pay_status = 0 then amount else 0 end ) as wait_amount,
        ");
        $this->db->from($this->table);
        $this->db->where('create_time >= '.$be['start_month']);
        $this->db->where('create_time <= '.$be['end_month']);
        return $this->db->get()->row();
    }

    public function getLatest(){
        $this->db->select("o.id,o.order_no,o.status,o.pay_status,c.name,u.nick_name,u.avatar,o.create_time");
        $this->db->from($this->table.' o');
        $this->db->join('user u','o.user_id = u.id','INNER');
        $this->db->join('car c','o.car_id = c.id','INNER');
        $this->db->order_by('id','desc');
        $this->db->limit(20);
        return $this->db->get()->result();
    }
}
